<template>
  <div class="mod-marketing-coupon">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @selection-change="selectionChange"
      @search-reset="searchReset"
      @refresh-change="refreshChange"
      @on-load="getDataList"
    >
      <template slot="search">
        <el-col :md="10" :xs="24">
          <el-form-item
            :label="$t('coupon.timeToMarket')"
            :label-width="this.$i18n.t('language') === 'language' ? '120px' : '80px'"
          >
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              :range-separator="$t('coupon.to')"
              value-format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="$t('coupon.startTime')"
              :end-placeholder="$t('coupon.endTime')"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </template>
      <template slot-scope="scope" slot="overdueStatus">
        <el-tag
          v-if="scope.row.overdueStatus === 1"
          type="success"
          size="small"
        >{{$t("coupon.notExpired")}}</el-tag>
        <el-tag v-else size="small" type="danger">{{$t("coupon.expired")}}</el-tag>
      </template>
      <template slot-scope="scope" slot="couponType">
        <el-tag v-if="scope.row.couponType == 1" size="small">{{$t("coupon.cashCoupon")}}</el-tag>
        <el-tag v-else-if="scope.row.couponType == 2" size="small">{{$t("coupon.discountVoucher")}}</el-tag>
        <el-tag v-else-if="scope.row.couponType == 3" size="small">{{$t("coupon.coinCertificate")}}</el-tag>
      </template>

      <template slot-scope="scope" slot="putonStatus">
        <el-tag
          v-if="scope.row.putonStatus === 1"
          type="success"
          size="small"
        >{{$t("coupon.putOnTheMarket")}}</el-tag>
        <el-tag
          v-else-if="scope.row.putonStatus === 0"
          size="small"
          type="warning"
        >{{$t("coupon.waitingForLaunch")}}</el-tag>
        <el-tag
          v-else-if="scope.row.putonStatus === -1"
          size="small"
          type="danger"
        >{{$t("coupon.cancelLaunch")}}</el-tag>
        <el-tag
          v-else-if="scope.row.putonStatus ===2"
          size="small"
          type="danger"
        >{{$t("coupon.violationOffShelf")}}</el-tag>
        <el-tag
          v-else-if="scope.row.putonStatus ===3"
          size="small"
          type="danger"
        >{{$t("coupon.waitingForReview")}}</el-tag>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-edit"
          type="text"
          size="small"
          @click="addOrUpdateHandle(scope.row.couponId)"
        >{{$t("coupon.viewDetails")}}</el-button>
        <el-button
          icon="el-icon-bottom"
          size="small"
          type="text"
          v-if="isAuth('platform:coupon:update') && scope.row.putonStatus<2"
          @click="offlineCouponHandle(scope.row)"
        >{{$t("coupon.offlineActivities")}}</el-button>
        <el-button
          icon="el-icon-document-checked"
          size="small"
          type="text"
          v-if="isAuth('platform:coupon:update') && scope.row.putonStatus > 1"
          @click="couponAuditHandle(scope.row.couponId)"
        >{{scope.row.putonStatus === 2?$t("coupon.violationOffShelf"):$t("coupon.checkPending")}}</el-button>
        <el-button
          icon="el-icon-delete"
          size="small"
          type="text"
          style="color:red"
          @click="deleteHandle(scope.row.couponId)"
        >{{$t("coupon.delete")}}</el-button>
      </template>
    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <!-- 商品审核弹窗 -->
    <CouponAudit
      v-if="couponAuditVisible"
      ref="couponAudit"
      selectUrl="/platform/coupon/getOfflineHandleEventByCouponId"
      updateUrl="/platform/coupon/auditCoupon"
      @refreshDataList="refreshChange"
    ></CouponAudit>
  </div>
</template>

<script>
import AddOrUpdate from './coupon-add-or-update'
import { tableOption } from '@/crud/marketing/coupon'
import CouponAudit from '@/components/offline-audit'
export default {
  data () {
    return {
      search: {
        slot: ''
      },
      dataForm: {
        orderNumber: '',
        status: null
      },
      dateRange: [],
      overdueOptions: [{
        value: 0,
        label: '过期'
      },
      {
        value: 1,
        label: '未过期'
      }],
      PutonOptions: [{
        value: -1,
        label: '取消投放'
      },
      {
        value: 0,
        label: '等待投放'
      },
      {
        value: 1,
        label: '已投放'
      }],
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      couponAuditVisible: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      tableOption: tableOption
    }
  },
  components: {
    AddOrUpdate,
    CouponAudit
  },
  methods: {
    // 获取数据列表
    getDataList (page, params) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/coupon/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize,
              'couponName': this.dataForm.couponName,
              'overdueStatus': this.dataForm.overdueStatus,
              'putonStatus': this.dataForm.putonStatus,
              'startTime': this.dateRange === null ? null : this.dateRange[0], // 开始时间
              'endTime': this.dateRange === null ? null : this.dateRange[1] // 结束时间
            },
            this.searchForm
          ), false
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    orderStatus () {

    },
    // 新增 / 修改
    addOrUpdateHandle (val) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(val)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(this.$t('coupon.Tips2') + `${id ? this.$t('coupon.delete') : this.$t('coupon.batchDelete')}` + this.$t('coupon.Tips3'), this.$t('coupon.TipsTitle'), {
        confirmButtonText: this.$t('coupon.confirm'),
        cancelButtonText: this.$t('coupon.cancel'),
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/platform/coupon'),
            method: 'delete',
            data: this.$http.adornData(id, false)
          }).then(({ data }) => {
            this.$message({
              message: this.$t('coupon.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.refreshChange()
              }
            })
          })
        })
        .catch(() => { })
    },
    // 条件查询
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 刷新回调用
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    // 多选变化
    selectionChange (val) {
      this.dataListSelections = val
    },
    // 清空自定义数据
    searchReset () {
      this.dateRange = []
    },
    // 下线活动
    offlineCouponHandle (row) {
      this.$prompt(this.$t('coupon.Tips0') + row.couponName + this.$t('coupon.Tips1'), this.$t('coupon.TipsTitle'), {
        confirmButtonText: this.$t('coupon.confirm'),
        cancelButtonText: this.$t('coupon.cancel')
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error(this.$t('coupon.offlineTimeCannotBeEmpty'))
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/coupon/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.couponId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: this.$t('coupon.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    // 弹窗处理
    couponAuditHandle (id) {
      this.couponAuditVisible = true
      this.$nextTick(() => {
        this.$refs.couponAudit.init(id)
      })
    }
  }
}
</script>
<style lang="scss">
.mod-order-order {
  .tit {
    display: flex;
    height: 45px;
    align-items: center;
  }
  .tit .item {
    padding: 0 10px;
    width: 10%;
    text-align: center;
  }
  .tit .product {
    width: 25%;
  }
  .prod-tit {
    padding: 10px;
    background: #f8f8f9;
    border-left: 1px solid #dddee1;
    border-top: 1px solid #dddee1;
    border-right: 1px solid #dddee1;
  }
  .prod-tit span {
    margin-right: 15px;
  }
  .prod-cont {
    display: flex;
    border-top: 1px solid #dddee1;
    border-bottom: 1px solid #dddee1;
    border-left: 1px solid #dddee1;
    color: #495060;
  }
  .prod-cont .item {
    display: flex;
    display: -webkit-flex;
    align-items: center;
    justify-content: center;
    padding: 10px;
    // width: 10%;
    border-right: 1px solid #dddee1;
    text-align: center;
    height: 100%;
  }
  .prod-cont .item span {
    display: block;
  }
  .prod-cont .prod-item {
    // width: 38%;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #dddee1;
  }
  .prod-name {
    width: 55%;
    text-align: left;
  }
  .prod-price {
    position: absolute;
    right: 40px;
    text-align: center;
  }
  .prod-price span {
    display: block;
    margin-bottom: 10px;
  }
  .prod-name .prod-info {
    display: block;
    color: #80848f;
    margin-top: 30px;
  }
  .prod-cont .items.name {
    display: flex;
    position: relative;
    padding: 20px;
    // height: 100px;
    border-bottom: 1px solid #dddee1;
  }
  .prod-cont .items.name:last-child {
    border-bottom: none;
  }
  .prod-image {
    margin-right: 20px;
    width: 100px;
    height: 100px;
  }
  .prod-image img {
    width: 100px;
    height: 100px;
  }
  .item span {
    display: block;
    margin-bottom: 10px;
  }
  .item .operate {
    color: #2d8cf0;
  }
  .item .totalprice {
    color: #c00;
  }
  .prod .remark {
    width: 100%;
    height: 50px;
    line-height: 50px;
    background-color: #e8f7f6;
    border-left: 1px solid #dddee1;
    border-right: 1px solid #dddee1;
    border-bottom: 1px solid #dddee1;
    margin-bottom: 20px;
  }
  .buyer-remark {
    padding: 0 20px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}
</style>
